package inspection

import (
	"database/sql"
	"github.com/panjf2000/ants/v2"
	metaD "db2s/MetaInit"
	cp "db2s/componentInit"
	"db2s/dbExec"
	"db2s/global"
	"db2s/inputArg"
	quit "db2s/quitDispos"
	mq "db2s/topic-mq"
	"os"
	"reflect"
	"sync"
	"time"
)

type (
	schemaInitContext SchemaInitContext
	// Topic 自定义Topic 类型 限制范围 避免出错
	Topic string
	Logo  string //dataFix Type 标识
	// CheckObject 自定义CheckObject 类型 限制范围 避免出错
	CheckObject string
	CheckMode   string // CheckMode
	IndexType   string //Index type distinction
	Keywords    string //data split keyword
)

const (
	syncIsSegmentQuery       Topic = "sycIsSegmentQuery"
	syncMissSegmentQuery     Topic = "syncMissSegmentQuery"
	syncIndexConcurrentQuery Topic = "syncIndexConcurrentQuery"
	syncQueryTableSql        Topic = "syncQueryTableSql"
	syncQueryTableData       Topic = "syncQueryTableData"
	syncGenerlInsert         Topic = "syncGenerlInsert"
	syncWriteInsert          Topic = "syncWriteInsert"
)

var (
	lock                    sync.Mutex
	SchemaContext           *schemaInitContext
	PlanContext             *SchedulePlanContext
	GlobalPConfigs          *GlobalParametersConfig
	GlobalISObjects         *GlobalISObject
	closeObject             quit.Close
	ResultStatisticsMq      chan any
	BarResultStatisticMq    chan any
	ResultStatisticsGeneral ResultGeneral
	//producer 生产者
	producer mq.Producer
	//consumer 消费者
	consumer                mq.Consumer
	FuncRef                 map[string]reflect.Value
	ScheduleTaskInitFuncRef map[string]reflect.Value
	MqListenerSInitFunc     map[string]reflect.Value
	terminalPods            map[string]*global.TPod
)

type (
	SchemaInitContextConfig struct {
		TableList     []string             //表对象清单
		GtCheckConfig *arg.ConfigParameter //客户端传参
	}
	// GlobalISObject 全局表结构或结构初始化对象
	GlobalISObject struct {
		TableMetaData  map[string]dbExec.TableStructMetadataer
		TableDataQuery map[string]dbExec.IndexColumnStruct
	}

	GlobalParametersConfig struct { // SchedulePlanConfig 输入参数重新复制
		TableList    []string
		SDB          map[string]*sql.DB //源端连接池
		DDB          map[string]*sql.DB //目标端连接池
		TtyBarFin    *os.File           //文件使用的时间戳
		schema       arg.SchemaS
		repair       arg.RepairS
		result       arg.Result
		rules        arg.RulesS
		dSns         arg.DSNsS
		structs      arg.StructS
		log          arg.LogS
		activeSwitch arg.ActiveSwitch
	}
	TableMetaDataDispos struct {
		LogThreadSeq  int64
		schemaStrconv global.TableMapStrconv
		tableStrconv  global.TableMapStrconv
	}
	CheckSchemaMetaData struct {
		TableMD       []*metaD.TableMeta
		ActiveSwitchV arg.ActiveSwitch
	}
	SchedulePlanContextConfig struct {
		TableList     []string
		BeginTime     time.Time
		GtCheckConfig arg.ConfigParameter
		MQConfig      *cp.MQConfig
	}
	TableAllColumnInfoS struct {
		SColumnInfo    []map[string]string //表的所有列信息
		DColumnInfo    []map[string]string //表的所有列信息
		Schema         string
		Table          string
		IndexColumnSeq map[string]string
		ColumnSeq      map[string]string
		MapTable       map[string]string
		IndexType      string
		IndexColumn    []string
		SshardName     string
		DshardName     string
	}
	mqProductCustomerMonitorMessage struct {
		mqProductSeq           *int64 //生产者信息
		mqCustomerSeq          *int64 //消费者数据记录
		ProducMessage          chan map[string][]string
		mqStartTiming          *int64
		mqLevelProductSend     *int64 //level 1生成的总数
		mqLevelCustomerReceive *int64
	}

	// SchedulePlan2 执行计划操作对象
	SchedulePlan2 struct {
		taskSeq                  int               //表校验序号
		schema, table            string            //check schema Name and check table Name
		mschema, mtable          string            //map check schema name and check table name
		indexColumn              []string          //check table index column
		indexColumnSeq           map[string]string //check table index column Seq
		indexColumnType          string            //check table index type
		logThreadSeq             int64             //log seq
		TotalCount               int64             //source and dest table max count
		tableLimit               int64
		chunkStartSource         string //schedule task type  value:src,dst,all
		repair                   arg.RepairS
		rules                    arg.RulesS
		idxc                     dbExec.IndexColumnStruct
		dstIdxc                  dbExec.IndexColumnStruct
		dbf                      dbExec.DataAbnormalFixStruct
		tableColData             TableAllColumnInfoS
		sshardName               string
		dshardName               string
		TableLocNumber           uint64
		errEnd                   *int64
		subTaskBeginTime         int64
		subTaskInfo              *mq.SubTasks
		sync                     global.SyncPlan
		mqProductCustomerMonitor mqProductCustomerMonitorMessage
	}
	// SchedulePlanContext 执行计划上下文
	SchedulePlanContext struct {
		//多任务执行计划
		ChanSchedulePlanTable any //[]map[string] /*SchemaToTable*/ *SchedulePlan2
		//存储执行计划任务
		SchedulePlanTable any //map[string] /*SchemaToTable*/ *SchedulePlan2
		// 上下文任务线程池
		Pool *ants.Pool
		//自动关闭mq的组件
		AutoCloseMqComponentTable map[cp.CheckMode]*cp.AutoCloseMqComponent
		//进度条
		TaskBarTable *mq.TaskBar
		Rw           sync.Mutex
		BeginTime    time.Time
		wg           sync.WaitGroup
		TableList    []string
		TableMiss    []string
	}
	// SchemaInitContext 执行计划上下文
	SchemaInitContext struct {
		TableList         []string
		TableMiss         []string
		TcolumnMeta       map[string]map[string]*metaD.TableStructMeta
		TDistributed      map[string]map[string]string
		IndexData         map[string]map[string]any
		TableSum          []string
		TablePartiton     map[string]global.Partition
		ChanSchemaContext []*SchemaInitContext
	}
	AutoCloseMqComponent struct {
		TopicStatistics  map[Topic]*int64
		UnsubscribeTopic []Topic
		CheckMode        CheckMode
		EndMark          bool
	}
)
